const http = require("../../utils/http");
const app = getApp();
// pages/play/play.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoUrl:"",
    detail:{},
    playItem:null,
    timeStr:null,
    timeList1:[],
    addCollect:"添加收藏",
    rating:"",
    userCommentList:[],
    len:0,
    isCollect:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  foramatTime(res){
    
    let dt = new Date(parseInt(res * 1000));
    let mm = dt.getMonth() + 1;
    let dd = dt.getDate();
    let h  = dt.getHours();
    let m  = dt.getMinutes();
    mm = mm < 10 ? "0" + mm : mm;
    dd = dd < 10 ? "0" + dd : dd;
    h  = h  < 10 ? "0" + h  : h ;
    m  = m  < 10 ? "0" + m  : m ;
    // console.log(mm,dd,h,m);
    return `${mm}-${dd} ${h}:${m}`
  },
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    })
    // console.log(options);
    let postid = options.postid;
    // 要使用代理
    // http.get(`https://app.vmovier.com/apiv3/post/view?postid=${postid}`).then(
    //   res => {
    //     console.log(res);
    //   }
    // )
    let _this = this;
    wx.request({
      url: "https://api.kele8.cn/agent?url=https://app.vmovier.com/apiv3/post/view?postid="+postid,
      success:res=>{
        wx.hideLoading()
        // console.log(res.data.data);
        let Timearr = res.data.data.comment.list;
        Timearr.forEach(item => {
          item.comTime = this.foramatTime(item.addtime);
        })
        // console.log(this.data.userCommentList);

        if(res.data.status == -1){
          wx.showLoading({
            title: '影片不存在',
          })
          return false;
        }
        // res.data.data.content_video[0].source_link
        let time = res.data.data.duration;
        let minu   = parseInt(time / 60);
        let second = parseInt(time  % 60);
        minu = minu < 10 ? "0" + minu : minu  ;
        second = second < 10 ? "0" + second : second;
        let timeStr = `${minu}'${second}'`

        let arr = res.data.data.relate_video;
        let newArr = [];
        arr.forEach(item => {
          // console.log(item);
          item.list.forEach(i => {
            let time = i.duration;
            // console.log(time);
            let minu  = parseInt(time / 60);
            let second = parseInt(time % 60);
            minu   = minu   < 10 ? "0" + minu   : minu  ;
            second = second < 10 ? "0" + second : second;
            let timeStr = `${minu}'${second}'`
            newArr.push(timeStr)
          })
        })
        

        this.setData({
          videoUrl:res.data.data.content_video[0].source_link,
          detail:res.data.data,
          playItem:res.data.data,
          timeStr:timeStr,
          timeList1:newArr,
          rating:res.data.data.rating,
          userCommentList:Timearr,
          len:res.data.data.count_comment
        })

        
        wx.cloud.database().collection("user_collect").get().then(
          re => {
            // console.log(re.data);
            let arr = re.data;
            arr.forEach(item => {
              if(res.data.data.postid == item.postid){
                this.setData({
                  addCollect:"已收藏"
                })
              }else {
                this.setData({
                  addCollect:"添加收藏"
                })
              }
            })
          }
        )
        // console.log(this.data.userCommentList);
        // console.log(_this.data.detail);
        // console.log(_this.data.userCommentList);
        // console.log(_this.data.videoUrl);
      }
    })

    
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  collect(){
    if(app.globalData.userInfo){
      const videoCollect = wx.cloud.database().collection("user_collect");
      videoCollect.where({
        postid:this.data.playItem.postid
      }).get().then(res => {
        if(res.data.length > 0 ){
          this.setData({
            addCollect:"已收藏"
          })
          wx.showToast({
            icon:"error",
            title:"您已收藏过"
          })
        }else {
          // 添加到收藏夹
          this.data.playItem.coltime = new Date().getTime();
          videoCollect.add({
            data:this.data.playItem
          }).then(
            res => {
              this.setData({
                addCollect:"已收藏",
                isCollect:true
              })
              wx.showToast({
                title: '收藏成功',
              });
            }
          ).catch(err => {
            wx.showToast({
              icon:"error",
              title: '收藏失败',
            })
          })
        }
      })
    }else {
      wx.showToast({
        icon:"error",
        title: '请先授权',
      })
      wx.getUserProfile({
        desc: '用于登录',
        success:res=> {
          // console.log(res);
          app.globalData.userInfo = res;
          wx.showToast({
            title: '授权成功',
          })
        },
        fail:err => {
          console.log("授权失败",err);
        }
      })
    }
  }
})